home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / lib / Semaphore.c < prev    next >
C/C++ Source or Header  |  1990-05-19  |  4KB  |  205 lines

  1. /* Semaphore.c -- implementation of Semaphore
  2.  
  3.     THIS SOFTWARE FITS THE DESCRIPTION IN THE U.S. COPYRIGHT ACT OF A
  4.     "UNITED STATES GOVERNMENT WORK".  IT WAS WRITTEN AS A PART OF THE
  5.     AUTHOR'S OFFICIAL DUTIES AS A GOVERNMENT EMPLOYEE.  THIS MEANS IT
  6.     CANNOT BE COPYRIGHTED.  THIS SOFTWARE IS FREELY AVAILABLE TO THE
  7.     PUBLIC FOR USE WITHOUT A COPYRIGHT NOTICE, AND THERE ARE NO
  8.     RESTRICTIONS ON ITS USE, NOW OR SUBSEQUENTLY.
  9.  
  10. Author:
  11.     K. E. Gorlen
  12.     Bg. 12A, Rm. 2033
  13.     Computer Systems Laboratory
  14.     Division of Computer Research and Technology
  15.     National Institutes of Health
  16.     Bethesda, Maryland 20892
  17.     Phone: (301) 496-1111
  18.     uucp: uunet!nih-csl!kgorlen
  19.     Internet: kgorlen@alw.nih.gov
  20.     December, 1985
  21.  
  22. Function:
  23.  
  24. Objects of class Semaphore are used to synchronize Processes.
  25.  
  26. $Log:    Semaphore.c,v $
  27.  * Revision 3.0  90/05/20  00:21:08  kgorlen
  28.  * Release for 1st edition.
  29.  * 
  30. */
  31.  
  32. #include "Semaphore.h"
  33. #include "Process.h"
  34. #include "Scheduler.h"
  35. #include "nihclIO.h"
  36.  
  37. #define    THIS    Semaphore
  38. #define    BASE    Object
  39. #define BASE_CLASSES BASE::desc()
  40. #define MEMBER_CLASSES LinkedList::desc()
  41. #define VIRTUAL_BASE_CLASSES Object::desc()
  42.  
  43. DEFINE_CLASS(Semaphore,1,"$Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/lib/RCS/Semaphore.c,v 3.0 90/05/20 00:21:08 kgorlen Rel $",NULL,NULL);
  44.  
  45. extern const int NIHCL_ASTBLK,NIHCL_DLSEMWAIT,NIHCL_CPSEMWAIT,NIHCL_STSEMWAIT;
  46.  
  47. Semaphore::Semaphore(int initialCount)    { count = initialCount; }
  48.  
  49. #ifndef BUG_TOOBIG
  50. // yacc stack overflow
  51.  
  52. Semaphore::Semaphore(const Semaphore& s) : waitList(s.waitList)
  53. {
  54.     count = s.count;
  55. }
  56.  
  57. #endif
  58.  
  59. void Semaphore::signal(unsigned n)
  60. {
  61.     if (n == 0) return;
  62.     AST_DISABLE;
  63.         register Process* next;
  64.         while (count<0 && n>0) {
  65.             next = Process::castdown(waitList.removeFirst());
  66.             if (next->state() != Process::TERMINATED) {
  67.                 next->resume();
  68.                 n--;
  69.             }
  70.             count++;
  71.         }
  72.         count += n;
  73.     AST_ENABLE;
  74. }
  75.  
  76. void Semaphore::wait()
  77. {
  78.     AST_DISABLE;
  79.         if (--count < 0) {
  80.             if (Scheduler::astActive()) setError(NIHCL_ASTBLK,DEFAULT,this,className(),"wait");
  81.             Scheduler::activeProcess().suspend();
  82.             waitList.addLast(Scheduler::activeProcess());
  83.         }
  84.     AST_ENABLE;
  85.     if (count < 0) Scheduler::schedule();
  86. }
  87.  
  88. Semaphore::~Semaphore()
  89. {
  90.     if (count<0) setError(NIHCL_DLSEMWAIT,DEFAULT,-count,this,className());
  91. }
  92.  
  93. Object* Semaphore::copy() const    { return deepCopy(); }
  94.  
  95. void Semaphore::deepenShallowCopy()
  96. {
  97.     if (count < 0) setError(NIHCL_CPSEMWAIT,DEFAULT,-count,this,className());
  98. }
  99.  
  100. unsigned Semaphore::hash() const    { return (unsigned)this; }
  101.  
  102. bool Semaphore::isEqual(const Object& ob) const    { return isSame(ob); }
  103.  
  104. void Semaphore::dumpOn(ostream& strm) const
  105. {
  106.     strm << className() << "[count: " << count << '\n';
  107.     DO(waitList,Process,p) p->dumpOn(strm); OD
  108.     strm << "]\n";
  109. }
  110.  
  111. void Semaphore::printOn(ostream& strm) const
  112. {
  113.     strm << "count: " << count;
  114.     if (count < 0) strm << "  waitList:";
  115.     DO(waitList,Process,p) strm << ' ' << *p; OD
  116. }
  117.  
  118. bool Semaphore::includes(const Process& p) const
  119. {
  120.     AST_DISABLE;
  121.     bool i = waitList.includes(p);
  122.     AST_ENABLE;
  123.     return i;
  124. }
  125.  
  126. Process* Semaphore::remove(Process& p)
  127. {
  128.     AST_DISABLE;
  129.     if (count < 0 && waitList.includes(p)) {
  130.         waitList.remove(p);
  131.         ++count;
  132.         AST_ENABLE;
  133.         return &p;
  134.     }
  135.     else {
  136.         AST_ENABLE;
  137.         return 0;
  138.     }
  139. }
  140.  
  141. Process* Semaphore::removeFirst()
  142. {
  143.     AST_DISABLE;
  144.     if (count < 0) {
  145.         Process* p = Process::castdown(waitList.removeFirst());
  146.         ++count;
  147.         AST_ENABLE;
  148.         return p;
  149.     }
  150.     else {
  151.         AST_ENABLE;
  152.         return 0;
  153.     }
  154. }
  155.  
  156. Process* Semaphore::removeLast()
  157. {
  158.     AST_DISABLE;
  159.     if (count < 0) {
  160.         Process* p = Process::castdown(waitList.removeLast());
  161.         ++count;
  162.         AST_ENABLE;
  163.         return p;
  164.     }
  165.     else {
  166.         AST_ENABLE;
  167.         return 0;
  168.     }
  169. }
  170.  
  171. int Semaphore::value() const    { return count; }
  172.  
  173. Semaphore::Semaphore(OIOin& strm)
  174.     : BASE(strm)
  175. {
  176.     strm >> count;
  177. }
  178.  
  179. void Semaphore::storer(OIOout& strm) const
  180. {
  181.     if (count < 0) setError(NIHCL_STSEMWAIT,DEFAULT,-count,this,className());
  182.     BASE::storer(strm);
  183.     strm << count;
  184. }
  185.  
  186. Semaphore::Semaphore(OIOifd& fd)
  187.     : BASE(fd)
  188. {
  189.     fd >> count;
  190. }
  191.  
  192. void Semaphore::storer(OIOofd& fd) const
  193. {
  194.     if ( count < 0 )
  195.         setError(NIHCL_STSEMWAIT,DEFAULT,-count,this,className());
  196.     BASE::storer(fd);
  197.     fd << count;
  198. }
  199.  
  200. int Semaphore::compare(const Object&) const
  201. {
  202.     shouldNotImplement("compare");
  203.     return 0;
  204. }
  205.